import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
import { map, Observable } from "rxjs";

interface data<T> {
    data: T
}

@Injectable()
export class ResponseInterceptor<T = any> implements NestInterceptor {

    /* 
        响应拦截，给他返回一个标准的json 格式
    */
    intercept(context: ExecutionContext, next: CallHandler<any>): Observable<data<T>> | Promise<Observable<data<T>>> {
        return next.handle().pipe(
            map(
                data => {
                    return {
                        data,
                        status: 0,
                        success: true,
                        message: '响应拦截成功，格式化输出数据'
                    }
                }
            )
        )
    }
}